home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / xvisrc.zip / PRESERVE.C < prev    next >
C/C++ Source or Header  |  1992-07-28  |  4KB  |  177 lines

  1. /* Copyright (c) 1990,1991,1992 Chris and John Downey */
  2. #ifndef lint
  3. static char *sccsid = "@(#)preserve.c    2.1 (Chris & John Downey) 7/29/92";
  4. #endif
  5.  
  6. /***
  7.  
  8. * program name:
  9.     xvi
  10. * function:
  11.     PD version of UNIX "vi" editor, with extensions.
  12. * module name:
  13.     preserve.c
  14. * module function:
  15.     Buffer preservation routines.
  16.  
  17.     The do_preserve() routine saves the contents of all modified
  18.     buffers in temporary files. It can be invoked with the
  19.     :preserve command or it may be called by one of the system
  20.     interface modules when at least PSVKEYS keystrokes have been
  21.     read, & at least Pn(P_preservetime) seconds have elsapsed
  22.     since the last keystroke. (PSVKEYS is defined in xvi.h.) The
  23.     preservebuf() routine can be used to preserve a single buffer.
  24.  
  25. * history:
  26.     STEVIE - ST Editor for VI Enthusiasts, Version 3.10
  27.     Originally by Tim Thompson (twitch!tjt)
  28.     Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
  29.     Heavily modified by Chris & John Downey
  30. ***/
  31.  
  32. #include "xvi.h"
  33.  
  34. /*
  35.  * Names of values for the P_preserve enumerated parameter.
  36.  */
  37. char    *psv_strings[] =
  38. {
  39.     "unsafe",
  40.     "standard",
  41.     "safe",
  42.     "paranoid",
  43.     NULL
  44. };
  45.  
  46. /*
  47.  * Open temporary file for given buffer.
  48.  */
  49. static FILE *
  50. psvfile(window)
  51. Xviwin    *window;
  52. {
  53.     register Buffer    *buffer;
  54.     FILE        *fp;
  55.  
  56.     buffer = window->w_buffer;
  57.  
  58.     if (buffer->b_tempfname == NULL) {
  59.     char    *fname;
  60.  
  61.     fname = buffer->b_filename;
  62.     if (fname == NULL)
  63.         fname = "unnamed";
  64.     buffer->b_tempfname = tempfname(fname);
  65.     if (buffer->b_tempfname == NULL) {
  66.         show_error(window, "Can't create name for preserve file");
  67.         return(NULL);
  68.     }
  69.     }
  70.     fp = fopenwb(buffer->b_tempfname);
  71.     if (fp == NULL) {
  72.     show_error(window, "Can't open preserve file %s",
  73.                          buffer->b_tempfname);
  74.     }
  75.     return(fp);
  76. }
  77.  
  78. /*
  79.  * Write contents of buffer to file & close file. Return TRUE if no
  80.  * errors detected.
  81.  */
  82. static bool_t
  83. putbuf(wp, fp)
  84. Xviwin        *wp;
  85. register FILE    *fp;
  86. {
  87.     unsigned long    l1, l2;
  88.  
  89.     if (put_file(wp, fp, (Line *) NULL, (Line *) NULL, &l1, &l2) == FALSE) {
  90.     show_error(wp, "Error writing preserve file %s",
  91.                          wp->w_buffer->b_tempfname);
  92.     return(FALSE);
  93.     } else {
  94.     return(TRUE);
  95.     }
  96. }
  97.  
  98. /*
  99.  * Preserve contents of a single buffer, so that a backup copy is
  100.  * available in case something goes wrong while the file itself is
  101.  * being written.
  102.  *
  103.  * This is controlled by the P_preserve parameter: if it's set to
  104.  * psv_UNSAFE, we just return. If it's psv_STANDARD, to save time, we
  105.  * only preserve the buffer if it doesn't appear to have been
  106.  * preserved recently: otherwise, if it's psv_SAFE or psv_PARANOID, we
  107.  * always preserve it.
  108.  *
  109.  * Return FALSE if an error occurs during preservation, otherwise TRUE.
  110.  */
  111. bool_t
  112. preservebuf(window)
  113. Xviwin    *window;
  114. {
  115.     FILE    *fp;
  116.     Buffer    *bp;
  117.  
  118.     if (
  119.     Pn(P_preserve) == psv_UNSAFE
  120.     ||
  121.     (
  122.         Pn(P_preserve) == psv_STANDARD
  123.         &&
  124.         /*
  125.          * If there is a preserve file already ...
  126.          */
  127.         (bp = window->w_buffer)->b_tempfname != NULL
  128.         &&
  129.         exists(bp->b_tempfname)
  130.         &&
  131.         /*
  132.          * & a preserve appears to have been done recently ...
  133.          */
  134.         keystrokes < PSVKEYS
  135.     )
  136.     ) {
  137.     /*
  138.      * ... don't bother.
  139.      */
  140.     return(TRUE);
  141.     }
  142.  
  143.     fp = psvfile(window);
  144.     if (fp == NULL) {
  145.     return(FALSE);
  146.     }
  147.  
  148.     return(putbuf(window, fp));
  149. }
  150.  
  151. /*
  152.  * Preserve contents of all modified buffers.
  153.  */
  154. bool_t
  155. do_preserve()
  156. {
  157.     Xviwin        *wp;
  158.     bool_t        psvstatus = TRUE;
  159.  
  160.     wp = curwin;
  161.     do {
  162.     if (is_modified(wp->w_buffer)) {
  163.         FILE    *fp;
  164.  
  165.         fp = psvfile(wp);
  166.         if (fp != NULL) {
  167.         if (!putbuf(wp, fp))
  168.             psvstatus = FALSE;
  169.         } else {
  170.         psvstatus = FALSE;
  171.         }
  172.     }
  173.     } while ((wp = next_window(wp)) != curwin);
  174.  
  175.     return(psvstatus);
  176. }
  177.